package io.zcoder.innoway.widgets;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.Point;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.zcoder.innoway.R;


public class ScrollSelectDateView extends LinearLayout implements View.OnTouchListener {

    private static final int DURATION_SCROLL = 300;
    private static final int TYPE_YEAR = 1;
    private static final int TYPE_MONTH = 2;
    private static final int TYPE_DAY = 3;
    private static final String[] MONTH = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};

    @BindView(R.id.scrollView)
    HorizontalScrollView mScrollView;
    @BindView(R.id.layout_date)
    LinearLayout mLayoutDateView;

    List<LinearLayout> mLayoutItemDate = new ArrayList<>();
    List<TextViewCustom> mTextDates = new ArrayList<>();
    List<View> mPointDates = new ArrayList<>();

    private Point mPoint = new Point();
    private SelectCallBack mSelectCallBack;
    private int mCurrentMonth;
    private int mCurrentYear = 1975;
    private int mCurrentIndex = 5;
    private int mCurrentType;
    private List<String> mCurrentValues;

    public ScrollSelectDateView(Context context) {
        super(context);
    }

    public ScrollSelectDateView(Context context, AttributeSet attrs) {
        super(context, attrs);
        View view = LayoutInflater.from(getContext()).inflate(R.layout.layout_date_picker_custom, this, true);
        ButterKnife.bind(view);
        TypedArray arr = context.obtainStyledAttributes(attrs, R.styleable.ScrollSelectDateView);
        mCurrentType = arr.getInt(R.styleable.ScrollSelectDateView_typeView, 1);

        WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
        wm.getDefaultDisplay().getSize(mPoint);

        initData();
    }

    private void initData() {
        mCurrentValues = new ArrayList<>();
        int year = Calendar.getInstance().get(Calendar.YEAR);
        if (1970 > year) {
            return;
        }
        if (mCurrentType == TYPE_MONTH) {
            mCurrentValues = new ArrayList<>(Arrays.asList(MONTH));
        } else if (mCurrentType == TYPE_YEAR) {
            for (int i = 1970; i < year + 1; i++) {
                mCurrentValues.add(i + "");
            }
        } else if (mCurrentType == TYPE_DAY) {
            Calendar calendar = new GregorianCalendar(mCurrentYear, mCurrentMonth, 1);
            int countMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
            for (int i = 0; i < countMonth; i++) {
                mCurrentValues.add((i + 1) + "");
            }
        }
        setCurrentIndex();
        initViews();
    }

    private void setCurrentIndex() {
        if (mCurrentIndex >= mCurrentValues.size()) {
            mCurrentIndex--;
            setCurrentIndex();
        }
    }

    public void setCallBack(SelectCallBack callBack) {
        mSelectCallBack = callBack;
    }

    public void setDayOfMonth(int value, int month, int year) {
        if (mCurrentType != TYPE_DAY) {
            return;
        }
        mCurrentMonth = month;
        mCurrentYear = year;
        mCurrentIndex = value - 1;
        initData();
    }

    public void setDayOfMonth(int month, int year) {
        mCurrentMonth = month;
        mCurrentYear = year;
        initData();
    }

    public void setValue(int value) {
        if (mCurrentType == TYPE_YEAR) {
            mCurrentIndex = mCurrentValues.indexOf("" + value);
            initData();
        } else if (mCurrentType == TYPE_MONTH) {
            mCurrentIndex = value - 1;
            initData();
        }
    }

    private void initViews() {
        LayoutParams params = new LayoutParams(mPoint.x / 2 - 1, 10);
        mLayoutDateView.removeAllViews();
        mTextDates = new ArrayList<>();
        mPointDates = new ArrayList<>();
        mLayoutItemDate = new ArrayList<>();
        mLayoutDateView.addView(new View(getContext()), params);
        for (int i = 0; i < mCurrentValues.size(); i++) {
            View view = View.inflate(getContext(), R.layout.item_date_picker_custom, null);
            mLayoutDateView.addView(view);
            LinearLayout layout = (LinearLayout) view.findViewById(R.id.layout_item_date);
            TextViewCustom textViewCustom = (TextViewCustom) view.findViewById(R.id.text_date);
            View viewPointDate = view.findViewById(R.id.point_date);
            mTextDates.add(textViewCustom);
            mPointDates.add(viewPointDate);
            mLayoutItemDate.add(layout);

            mTextDates.get(i).setText(mCurrentValues.get(i));
        }
        mLayoutDateView.addView(new View(getContext()), params);

        mScrollView.setOnTouchListener(this);
        scrollToPoint(false);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_UP) {
            scrollToPoint(true);
            return true;
        }
        return false;
    }

    private void scrollToPoint(boolean auto) {
        for (int i = 0; i < mCurrentValues.size(); i++) {
            int[] location = {0, 0};
            mLayoutItemDate.get(i).getLocationOnScreen(location);
            if ((!auto && mCurrentIndex == i) || (location[0] <= mPoint.x / 2 && location[0] + getResources().getDimension(R.dimen.width_item_date) >= mPoint.x / 2)) {
                mTextDates.get(i).setTextColor(Color.WHITE);
                mPointDates.get(i).setBackgroundResource(R.color.bg_select_tall_btn);

                float positionScroll = getResources().getDimension(R.dimen.width_item_date) * (i + 0.5f) - mPoint.x / 2 + mPoint.x / 2 - 1;
                ObjectAnimator animator =
                        ObjectAnimator.ofInt(mScrollView, "scrollX", (int) positionScroll);
                animator.setDuration(DURATION_SCROLL);
                animator.start();
                mCurrentIndex = i;

                if (mSelectCallBack != null) {
                    mSelectCallBack.onSelected();
                }


            } else {
                mTextDates.get(i).setTextColor(getResources().getColor(R.color.gray63));
                mPointDates.get(i).setBackgroundResource(R.color.select_birthday_blue_light);
            }
        }
    }

    public String getValue() {
        return mCurrentValues.get(mCurrentIndex);
    }

    public int getIndex() {
        return mCurrentIndex;
    }

    public interface SelectCallBack {
        void onSelected();
    }
}
